#!/bin/bash
#
# rollback-dual-user.sh - 回滚双用户转换
#
# 功能：
# - 列出可用的 Snapper 快照
# - 选择快照并回滚
# - 提供手动回滚说明
#
# 使用方法：
# sudo bash rollback-dual-user.sh
#

set -e

##############################################################################
# 颜色和输出函数
##############################################################################

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[警告]${NC} $1"
}

log_error() {
    echo -e "${RED}[错误]${NC} $1"
}

##############################################################################
# 主流程
##############################################################################

echo ""
echo "========================================"
echo "  双用户方案回滚工具"
echo "========================================"
echo ""

# 检查 root 权限
if [ "$EUID" -ne 0 ]; then
    log_error "请使用 sudo 或 root 身份运行此脚本"
    exit 1
fi

# 检查 Snapper
if ! command -v snapper &> /dev/null; then
    log_error "Snapper 未安装"
    echo ""
    log_info "请手动回滚，步骤如下："
    echo ""
    echo "1. 重启到 Arch ISO Live 环境"
    echo ""
    echo "2. 挂载 Btrfs 根分区:"
    echo "   mount /dev/sda2 /mnt"
    echo ""
    echo "3. 重命名子卷:"
    echo "   mv /mnt/@home_lin /mnt/@home"
    echo "   # (将 lin 替换为您的主用户名)"
    echo ""
    echo "4. 删除测试用户子卷:"
    echo "   btrfs subvolume delete /mnt/@home_test"
    echo "   btrfs subvolume delete /mnt/@home_shared"
    echo ""
    echo "5. 恢复 fstab:"
    echo "   mount /dev/sda1 /mnt/boot/efi"
    echo "   arch-chroot /mnt"
    echo "   # 编辑 /etc/fstab，恢复为原来的配置"
    echo "   # 或从备份恢复: cp /etc/fstab.bak.* /etc/fstab"
    echo ""
    echo "6. 删除测试用户:"
    echo "   userdel -r test"
    echo ""
    echo "7. 退出 chroot，卸载并重启:"
    echo "   exit"
    echo "   umount -R /mnt"
    echo "   reboot"
    echo ""
    exit 1
fi

# 检查 Snapper 配置
if ! snapper list-configs | grep -q "root"; then
    log_error "Snapper 未配置"
    log_info "无法使用自动回滚"
    exit 1
fi

# 显示快照列表
log_info "查找与双用户转换相关的快照..."
echo ""
echo "可用快照列表:"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
snapper list | grep -E "(Number|转换双用户|setup-dual-user)" || snapper list | tail -10
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

# 查找转换快照
CONVERT_SNAPSHOT=$(snapper list | grep "转换双用户方案前" | awk '{print $1}' | tail -1)

if [ -n "$CONVERT_SNAPSHOT" ]; then
    log_info "检测到转换快照，ID: $CONVERT_SNAPSHOT"
    echo ""
    read -p "是否回滚到此快照？ [Y/n] " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Nn]$ ]]; then
        log_info "请手动输入快照 ID"
        read -p "输入快照 ID: " SNAPSHOT_ID
    else
        SNAPSHOT_ID=$CONVERT_SNAPSHOT
    fi
else
    log_warn "未找到转换快照"
    echo ""
    read -p "请输入要回滚到的快照 ID: " SNAPSHOT_ID
fi

# 验证快照 ID
if ! snapper list | grep -q "^$SNAPSHOT_ID "; then
    log_error "快照 ID $SNAPSHOT_ID 不存在"
    exit 1
fi

# 显示快照详情
echo ""
log_info "快照详情:"
snapper info "$SNAPSHOT_ID"
echo ""

# 确认回滚
log_warn "警告：回滚操作将撤销所有自快照创建以来的更改"
echo ""
read -p "确认回滚到快照 $SNAPSHOT_ID？ [y/N] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    log_info "已取消回滚"
    exit 0
fi

# 执行回滚
echo ""
log_info "开始回滚..."
log_info "执行命令: snapper undochange ${SNAPSHOT_ID}..0"
echo ""

snapper undochange "${SNAPSHOT_ID}..0"

log_info "回滚完成！"
echo ""
echo "========================================"
echo "  回滚成功"
echo "========================================"
echo ""
echo "下一步："
echo "  1. 重启系统使更改生效"
echo "     sudo reboot"
echo ""
echo "  2. 重启后，系统将恢复到转换前的状态"
echo ""
echo "  3. 验证系统："
echo "     - 检查用户: ls /home/"
echo "     - 检查子卷: sudo btrfs subvolume list /"
echo ""
echo "========================================"
echo ""

read -p "是否立即重启？ [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    log_info "正在重启..."
    sleep 2
    reboot
else
    log_info "请稍后手动重启: sudo reboot"
fi

